package com.gouda.core.dao;

import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.gouda.core.dto.order.OrderQueryParamDto;
import com.gouda.core.entity.OrderEntity;
import lombok.extern.slf4j.Slf4j;
import org.amethyst.atlas.db.MagicDao;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by KGL on 2015/12/7.
 */
@Slf4j
@Repository
public class OrderDao extends MagicDao {
    private static final String QUERY_SQL = "select * from orders t ";


    private static final String QUERY_COUNT = "select count(1) from orders t ";


    /**
     * 根据条件查询符合条件总的记录数
     *
     * @param orderQueryParamDto
     * @return
     */
    public int getCountByConditions(OrderQueryParamDto orderQueryParamDto) {
        StringBuilder whereSql = new StringBuilder("where 1=1 ");
        final Map<String, Object> params = Maps.newHashMap();
        if (null != orderQueryParamDto) {
            if (null != orderQueryParamDto.getServiceType()) {
                whereSql.append(" and t.service_type = :service_type ");
                params.put("service_type", orderQueryParamDto.getServiceType());
            }
            if (null != orderQueryParamDto.getUid()) {
                whereSql.append(" and t.uid = :uid ");
                params.put("uid", orderQueryParamDto.getUid());
            }
            if (null != orderQueryParamDto.getMerchant()) {
                whereSql.append(" and t.merchant = :merchant ");
                params.put("merchant", orderQueryParamDto.getMerchant());
            }
            if (null != orderQueryParamDto.getState()) {
                whereSql.append(" and t.state = :state ");
                params.put("state", orderQueryParamDto.getState());
            }
            if (null != orderQueryParamDto.getOrderType()) {
                whereSql.append(" and t.order_type = :order_type ");
                params.put("order_type", orderQueryParamDto.getOrderType());
            }
            if (null != orderQueryParamDto.getUcId()) {
                whereSql.append(" and t.uc_id = :uc_id ");
                params.put("uc_id", orderQueryParamDto.getUcId());
            }

            if (Strings.isNullOrEmpty(orderQueryParamDto.getOrderCode())) {
                whereSql.append(" and t.order_code = :order_code ");
                params.put("order_code", orderQueryParamDto.getOrderCode());
            }
            if (Strings.isNullOrEmpty(orderQueryParamDto.getCreateStartTime())) {
                whereSql.append(" and t.create_time >= :create_time ");
                params.put("create_time", orderQueryParamDto.getCreateStartTime());
            }
            if (Strings.isNullOrEmpty(orderQueryParamDto.getCreateEndTime())) {
                whereSql.append(" and t.create_time <= :create_end_time ");
                params.put("create_end_time", orderQueryParamDto.getCreateEndTime());
            }
        }
        return super.count(OrderEntity.class, QUERY_COUNT + whereSql.toString(), params);
    }

    /**
     * 查询符合条件的数据
     *
     * @param orderQueryParamDto
     * @param start
     * @param size
     * @return
     */
    public List<OrderEntity> getListByConditions(OrderQueryParamDto orderQueryParamDto, final int start, final int size) {
        StringBuilder whereSql = new StringBuilder("where 1=1 ");
        final Map<String, Object> params = Maps.newHashMap();
        if (null != orderQueryParamDto) {
            if (null != orderQueryParamDto.getServiceType()) {
                whereSql.append(" and t.service_type = :service_type ");
                params.put("service_type", orderQueryParamDto.getServiceType());
            }
            if (null != orderQueryParamDto.getUid()) {
                whereSql.append(" and t.uid = :uid ");
                params.put("uid", orderQueryParamDto.getUid());
            }
            if (null != orderQueryParamDto.getMerchant()) {
                whereSql.append(" and t.merchant = :merchant ");
                params.put("merchant", orderQueryParamDto.getMerchant());
            }
            if (null != orderQueryParamDto.getState()) {
                whereSql.append(" and t.state = :state ");
                params.put("state", orderQueryParamDto.getState());
            }
            if (null != orderQueryParamDto.getOrderType()) {
                whereSql.append(" and t.order_type = :order_type ");
                params.put("order_type", orderQueryParamDto.getOrderType());
            }
            if (null != orderQueryParamDto.getUcId()) {
                whereSql.append(" and t.uc_id = :uc_id ");
                params.put("uc_id", orderQueryParamDto.getUcId());
            }

            if (Strings.isNullOrEmpty(orderQueryParamDto.getOrderCode())) {
                whereSql.append(" and t.order_code = :order_code ");
                params.put("order_code", orderQueryParamDto.getOrderCode());
            }
            if (Strings.isNullOrEmpty(orderQueryParamDto.getCreateStartTime())) {
                whereSql.append(" and t.create_time >= :create_time ");
                params.put("create_time", orderQueryParamDto.getCreateStartTime());
            }
            if (Strings.isNullOrEmpty(orderQueryParamDto.getCreateEndTime())) {
                whereSql.append(" and t.create_time <= :create_end_time ");
                params.put("create_end_time", orderQueryParamDto.getCreateEndTime());
            }
        }
        whereSql.append(" order by t.create_time desc limit :start,:size");
        params.put("start", start);
        params.put("size", size);
        return super.list(OrderEntity.class, QUERY_SQL + whereSql.toString(), params);
    }


}
